package com.jnu.myaccount.function.detail;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.jnu.myaccount.R;
import com.jnu.myaccount.data.Data;
import com.jnu.myaccount.data.DataBank.DataBank;
import com.jnu.myaccount.data.EcharOptionUtil;
import com.jnu.myaccount.data.EchartView;
import com.jnu.myaccount.databinding.FragmentSlideshowBinding;

import java.util.ArrayList;
import java.util.Calendar;

public class SlideshowFragment extends Fragment implements View.OnClickListener {

    private SlideshowViewModel slideshowViewModel;
    private FragmentSlideshowBinding binding;
    private EchartView lineChart;
    private EchartView lineChart2;

    private ImageView selectLeft;//左箭头
    private ImageView selectRight;//右箭头
    private TextView selectYear;//年份选择

    //数据
    private  int year;//年份
    private int month;//月份

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentSlideshowBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        Calendar date = Calendar.getInstance();
        year=date.get(Calendar.YEAR);
        month=date.get(Calendar.MONTH)+1;
        //绑定控件
        selectLeft=root.findViewById(R.id.history_left_iv);
        selectRight=root.findViewById(R.id.history_right_iv);
        selectYear=root.findViewById(R.id.history_year_select);
        selectYear.setText(year+"年"+month+"月");
        selectRight.setOnClickListener(this);
        selectLeft.setOnClickListener(this);
        lineChart = root.findViewById(R.id.lineChart);
        lineChart.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshLineChart();
            }
        });



        return root;
    }

    private void refreshLineChart2() {
        DataBank mDataBank=new DataBank(getActivity());
        ArrayList<Data> mList=mDataBank.loadData();

        if(mList!=null) {
            ArrayList<Data> mListMiddle=new ArrayList<>();

            for(int i=0;i<mList.size();i++){
                if(mList.get(i).year==year)
                    if(mList.get(i).month==month)
                    {

                            mListMiddle.add(mList.get(i));

                    }
            }

            if(mListMiddle.size()!=0){
                Object[] x =new Object[mListMiddle.size()];
                Object[] y = new Object[mListMiddle.size()];
                for(int j=0;j<mListMiddle.size();j++){
                    x[j] =mListMiddle.get(j).getYearMonth();
                    y[j] =mListMiddle.get(j).itemCost;
                }
                lineChart.refreshEchartsWithOption(EcharOptionUtil.getLineChartOptions(x, y));
            }

        }

    }

    private void refreshLineChart() {

        DataBank mDataBank=new DataBank(getActivity());
        ArrayList<Data> mList=mDataBank.loadData();

        if(mList!=null) {
            ArrayList<Data> mListMiddle=new ArrayList<>();

            for(int i=0;i<mList.size();i++){
                if(mList.get(i).year==year)
                    if(mList.get(i).month==month)
                    {
                        if(mList.get(i).itemCost<0) {
                            mListMiddle.add(mList.get(i));

                        }
                    }
            }

            if(mListMiddle.size()!=0){
                Object[] x =new Object[mListMiddle.size()];
                Object[] y = new Object[mListMiddle.size()];
                for(int j=0;j<mListMiddle.size();j++){
                    x[j] =mListMiddle.get(j).getYearMonth();
                    y[j] =mListMiddle.get(j).itemCost*-1;
                }
                lineChart.refreshEchartsWithOption(EcharOptionUtil.getLineChartOptions(x, y));
            }
            else{
                Object[] x =new Object[]{};
                Object[] y = new Object[]{};
                lineChart.refreshEchartsWithOption(EcharOptionUtil.getLineChartOptions(x, y));
            }
          /* Object[] i = new Object[]{
                    "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "222"
            };
            Object[] j = new Object[]{
                    820, 932, 901, 934, 1290, 1330, 1320, 2500
            };*/
        }
        else{
            Object[] x =new Object[]{};
            Object[] y = new Object[]{};
            lineChart.refreshEchartsWithOption(EcharOptionUtil.getLineChartOptions(x, y));
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.history_left_iv:
                month--;
                if(month==0){
                    year--;
                    month=12;
                }
                selectYear.setText(year+"年"+month+"月");
                refreshLineChart();
                break;
            case R.id.history_right_iv:
                month++;
                if(month==13){
                    year++;
                    month=1;
                }
                selectYear.setText(year+"年"+month+"月");
                refreshLineChart();
                break;
        }

    }
}

